/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.montecristo.posmobile;


import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import org.montecristo.posmobile.previo.base.ManagerDB;
import org.montecristo.posmobile.previo.clases.encuesta.Applicacion;
import org.montecristo.posmobile.previo.clases.encuesta.Seccion;
import org.montecristo.posmobile.R;


public class HeadlinesFragment extends ListFragment implements OnItemClickListener {
   
	private ManagerDB manager;
	private Applicacion app;
    List<Seccion> mHeadlinesList = new ArrayList<Seccion>();
    ArrayAdapter<Seccion> mListAdapter;
    OnHeadlineSelectedListener mHeadlineSelectedListener = null;
    public interface OnHeadlineSelectedListener {
        public void onHeadlineSelected(int index);
    }

    public HeadlinesFragment() {
        super();
    }

   @Override
    public void onStart() {
        super.onStart();
        setListAdapter(mListAdapter);
        getListView().setOnItemClickListener(this);
        loadCategory(0);
    }

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        mListAdapter = new ArrayAdapter<Seccion>(getActivity(), R.layout.headline_item,mHeadlinesList);
        manager = new ManagerDB(this.getActivity());
        String id=getActivity().getIntent().getStringExtra("idApp");
        app= manager.getApplicacionById(id);
    }


    public void setOnHeadlineSelectedListener(OnHeadlineSelectedListener listener) {
        mHeadlineSelectedListener = listener;
    }


    public void loadCategory(int categoryIndex) {
      	mHeadlinesList=manager.getSeccionesBYEncuesta(app.getIdEncuesta());
    	mListAdapter = new ArrayAdapter<Seccion>(getActivity(), R.layout.headline_item, mHeadlinesList);
    	setListAdapter(mListAdapter);
    }


   @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (null != mHeadlineSelectedListener) {
            mHeadlineSelectedListener.onHeadlineSelected(position);
        }
    }


    public void setSelectable(boolean selectable) {
        if (selectable) {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
        else {
            getListView().setChoiceMode(ListView.CHOICE_MODE_NONE);
        }
    }
}
